package com.shop.admin.controller;

import com.shop.admin.entity.SysMenu;
import com.shop.admin.entity.SysRole;
import com.shop.admin.result.ApiResult;
import com.shop.admin.service.SysRoleService;
import com.shop.admin.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Api(tags = {"SysRoleController"},description = "角色管理接口")
@RestController
@RequestMapping("/api/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    @ApiOperation("角色列表")
    @GetMapping("/list")
    public ApiResult roleList(@ApiParam("参数为：page,limit（可选）.返回（roleId,roleName（角色名称）,roleCode（角色编码）,description(描述)，创建时间）")@RequestParam Map<String, Object> params) {
        PageUtils page = sysRoleService.getRolesList(params);
        return ApiResult.ok(page);
    }

    @ApiOperation("角色修改和新增")
    @PostMapping("/save")
    public ApiResult roleSave(@ApiParam("参数为：id,roleName（角色名称）,roleCode（角色编码）,description(描述)")@RequestBody SysRole sysRole) {
        if (sysRole.getId() != null) {
            //修改
            if (sysRoleService.roleUpdateBoolean(sysRole.getRoleName(), sysRole.getRoleCode(), sysRole.getId())) {
                sysRole.setUpdateTime(new Date());
                sysRoleService.updateById(sysRole);
                return ApiResult.ok("修改成功");
            }
        }else{
            //新增
            if (sysRoleService.roleSaveBoolean(sysRole.getRoleName(), sysRole.getRoleCode())) {
                sysRole.setCreateTime(new Date());
                sysRole.setIsDeleted(0);
                sysRoleService.save(sysRole);
                return ApiResult.ok("新增成功");
                }
            }
            return ApiResult.ok("数据重复，请修改数据");
        }

    //删除角色
    @PostMapping("/delete")
    @ApiOperation(value = "角色的删除",notes = "id,id可为数组类型")
    public ApiResult roleDelete(@ApiParam("参数为：id[]") @RequestBody Long[] ids){
        sysRoleService.removeRoleByIds(Arrays.asList(ids));
        return ApiResult.ok("删除成功！");
    }


}
